package com.rljk.controller.app;

import com.alibaba.fastjson.JSON;
import com.rljk.common.consts.SexEnum;
import com.rljk.common.controller.BaseController;
import com.rljk.common.result.CodeMsg;
import com.rljk.common.result.Result;
import com.rljk.config.ThirdLoginConfig;
import com.rljk.domain.common.PageData;
import com.rljk.redis.RedisService;
import com.rljk.service.impl.OpenuserService;
import com.rljk.service.impl.ThirdLoginService;
import com.rljk.utils.HttpClientUtils;
import com.rljk.utils.Tools;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.annotation.Resource;

/**
 * Author:   YASUO
 * Date:     2018/8/7 14:31
 * Description:第三方登陆
 */
@RestController
@RequestMapping("/thirdlogin")
@Slf4j
public class ThirdLoginInController extends BaseController{

    @Autowired
    private ThirdLoginConfig constants;

    @Autowired
    RedisService redisService;

    @Resource(name = "openuserService")
    private OpenuserService openuserService;

    @Resource(name = "thirdLoginService")
    private ThirdLoginService thirdLoginService;



    /**
     * qq获取授权码
     */
    @RequestMapping("getqqcode")
    public Object getQQCode() {
        log.info("qq获取授权码");
        try {
            String qQoCode = thirdLoginService.getQQoCode();
            return Result.success(qQoCode);
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.error(CodeMsg.SERVER_ERROR);
    }


    /**
     * qq获取用户信息
     */
    @RequestMapping("getqquserinfo")
    public Object getUserInfo(String code) {
        log.info("临时票据code:"+code);
        if(Tools.isEmpty(code)){
            Result.error(CodeMsg.PARAM_LACK);
        }
        try {
            PageData userInfo = (PageData)thirdLoginService.getQQUserInfo(code);
            PageData pd = new PageData();

            if(userInfo.getString("SEX").equals("1")){
                pd.put("SEX", SexEnum.MAN.getName());
            }else if(userInfo.getString("SEX").equals("2")){
                pd.put("SEX", SexEnum.WOMAN.getName());
            }else if(userInfo.getString("SEX").equals("0")){
                pd.put("SEX", SexEnum.UNKNOWN.getName());
            }
            pd.put("NICKNAME", userInfo.get("NICKNAME"));
            pd.put("PORTRAIT", userInfo.get("PORTRAIT"));

            String TOKEN = this.getSnowId();
            redisService.setex(TOKEN, userInfo.getString("OPENUSER_ID"),60*60*24*30);
            pd.put("TOKEN", TOKEN);
            return Result.success(pd);
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.error(CodeMsg.SERVER_ERROR);
    }


    /**
     * 微信第三方获取登录二维码(前端做了)
     */
    @RequestMapping("getweixinqrcode")
    public Object getWeChatQrcode() {
        log.info("微信第三方登录获取用户信息");
        try {
            String wechatCode = thirdLoginService.getWechatCode();
            //wechatCode.replace("/connect/qrcode","https://open.weixin.qq.com/connect/qrcode");

            return "https://open.weixin.qq.com/connect/qrconnect?appid=wx4cd7b2b3aba3aea5&redirect_uri=http%3A%2F%2Fh5.zgjszl.com%2F%23%2F&response_type=code&scope=snsapi_login";
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.error(CodeMsg.SERVER_ERROR);
    }

    /**
     * 微信第三方登录获取用户信息
     */
    @RequestMapping("getweixinuserinfo")
    public Object getWeChatUserInfo(String code) {
        log.info("微信第三方登录获取用户信息");
        if(Tools.isEmpty(code)){
            Result.error(CodeMsg.PARAM_LACK);
        }
        try {
            PageData userInfo = (PageData)thirdLoginService.getWechatUserInfo(code);
            PageData pd = new PageData();

            pd.put("NICKNAME", userInfo.get("NICKNAME"));
            pd.put("SEX", getSex(userInfo));
            pd.put("PORTRAIT", userInfo.get("PORTRAIT"));

            String TOKEN = this.getSnowId();
            redisService.setex(TOKEN, userInfo.getString("OPENUSER_ID"),60*60*24*30);
            pd.put("TOKEN", TOKEN);
            return Result.success(pd);
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.error(CodeMsg.SERVER_ERROR);
    }

    /**
     * 微博第三方登录获取用户信息
     */
    @RequestMapping("getweibouserinfo")
    public Object getWeiboUserInfo(String code) {
        log.info("微博第三方登录获取用户信息");
        if(Tools.isEmpty(code)){
            Result.error(CodeMsg.PARAM_LACK);
        }
        try {
            String weiBoAccessToken = thirdLoginService.getWeiBoAccessToken(code);
            PageData userInfo = (PageData)thirdLoginService.getWeiBoUserInfo(weiBoAccessToken);
            PageData pd = new PageData();

            pd.put("NICKNAME", userInfo.get("NICKNAME"));
            pd.put("SEX", getSex(userInfo));
            pd.put("PORTRAIT", userInfo.get("PORTRAIT"));

            String TOKEN = this.getSnowId();
            redisService.setex(TOKEN, userInfo.getString("OPENUSER_ID"),60*60*24*30);
            pd.put("TOKEN", TOKEN);
            return Result.success(pd);
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.error(CodeMsg.SERVER_ERROR);
    }




    private static String getSex(PageData userInfo){
        if(userInfo.getString("SEX").equals("1")){
            return SexEnum.MAN.getName();
        }else if(userInfo.getString("SEX").equals("2")){
            return SexEnum.WOMAN.getName();
        }else if(userInfo.getString("SEX").equals("0")){
            return SexEnum.UNKNOWN.getName();
        }
        return null;
    }








}
